package com.tj.admin.rest.finance;

import com.alibaba.fastjson.JSON;
import com.fqgj.exception.common.ApplicationException;
import com.robert.vesta.util.IdUtils;
import com.tj.admin.auth.anno.VisitorAccessible;
import com.tj.admin.rest.finance.request.WithDrawAuditCallbackRequest;
import com.tj.bo.account.client.WithdrawService;
import com.tj.bo.account.client.enums.WithDrawStatusEnum;
import com.tj.bo.account.client.vo.WithDrawAuditCallbackVO;
import com.tj.bo.account.client.vo.WithDrawPayoutNotifyVO;
import com.tj.bo.account.client.vo.WithdrawRecordVO;
import com.tj.bo.common.conf.api.ApiResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * User: binary (mr.vencnet@gmail.com)
 * Date: 2020-08-11
 * Time: 17:12
 */
@RestController
@RequestMapping("/withdraw")
public class WithdrawController {

    private static final Logger LOGGER = LoggerFactory.getLogger(WithdrawController.class);

    @Autowired
    private WithdrawService withdrawService;

    /**
     * 提现结果异步回调
     */
    @RequestMapping("/audit/notify")
    @VisitorAccessible
    public ApiResponse withDrawAuditNotify(@RequestBody WithDrawAuditCallbackRequest withDrawAuditCallbackRequest) {
        LOGGER.info("审核结果异步回调,withDrawAuditCallbackVO:{}", JSON.toJSONString(withDrawAuditCallbackRequest));
        if (StringUtils.isEmpty(withDrawAuditCallbackRequest.getAuditor())) {
            withDrawAuditCallbackRequest.setAuditor("auditor");
        }
        WithDrawAuditCallbackVO withDrawAuditCallbackVO = new WithDrawAuditCallbackVO();
        BeanUtils.copyProperties(withDrawAuditCallbackRequest, withDrawAuditCallbackVO);
        withdrawService.withDrawAuditNotify(withDrawAuditCallbackVO);
        return new ApiResponse();
    }

    @RequestMapping("/updateStatus")
    public ApiResponse updateWithdraw(@RequestBody WithdrawRequest request) {

        if (request.getWithdrawNo() == null || request.getStatus() == null) {
            throw new ApplicationException("wrong request");
        }

        WithdrawRecordVO withdrawRecordByWithdrawNo = withdrawService.getWithdrawRecordByWithdrawNo(request.getWithdrawNo());
        if (withdrawRecordByWithdrawNo == null) {
            return new ApiResponse("application not exist");
        }

        if (withdrawRecordByWithdrawNo.getStatusCode() == request.getStatus()) {
            return new ApiResponse("update success");
        }

        WithDrawStatusEnum withDrawStatusEnum = WithDrawStatusEnum.getByCode(request.getStatus());
        if (withDrawStatusEnum == null) {
            throw new ApplicationException("status change failed");
        }


        //等待提现->提现成功、失败
        if (withdrawRecordByWithdrawNo.getStatusCode() == (WithDrawStatusEnum.AUDIT_PROCESSING.getCode())) {
            withdrawService.updateWithdrawStatusByWithdrawNo(request.getWithdrawNo(), WithDrawStatusEnum.PAYOUT_PROCESSING.getCode());

            WithDrawPayoutNotifyVO withDrawPayoutNotifyVO = new WithDrawPayoutNotifyVO()
                    .setAmount(withdrawRecordByWithdrawNo.getAmount())
                    .setCode(withDrawStatusEnum.equals(WithDrawStatusEnum.PAYOUT_SUCCESS) ? "0000" : "0001")
                    .setMessage("web")
                    .setPayAcceptNo(IdUtils.genId() + "")
                    .setPaymentChannelName("-2")
                    .setWithdrawOrderNo(request.getWithdrawNo());

            withdrawService.withDrawPayoutNotify(withDrawPayoutNotifyVO);
            return new ApiResponse();
        } else {
            return new ApiResponse();
        }
    }
}
